package com.cljh.bean.Chat;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Date;

/**
 * 封装消息的工具类
 */
public class MessageUtils {
    public static String getMessage(boolean isSystemMessage, String fromName, Object message, String img, Integer id,boolean online){
        try{
            ResultMessage resultMessage = new ResultMessage();
            resultMessage.setSystem(isSystemMessage);
            resultMessage.setMessage(message);
            resultMessage.setOnline(online);

            if (fromName!=null){
                resultMessage.setFromName(fromName);
            }

            if (img!=null){
                resultMessage.setImg(img);
            }
            if (id!=null){
                resultMessage.setId(id);
                resultMessage.setSendTime(new Date());
            }

            //创建mapper转化为json格式
            ObjectMapper mapper = new ObjectMapper();

            return mapper.writeValueAsString(resultMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
